package batch.home;

import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.String;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.*;

import batch.common.report.*;

public class makeHomeForMex
{
    private final int    ILENG       = 5;
    private final String TODAY       = DateFormatUtils.format(new Date(), "yyyyMMdd");  //Get Current Date

    private final String MAILSUBJECT = "MEX HOME Batch Error(" + TODAY + ")";
    private final String FILE_PATH   = "/home01/sfc/batch/home/data/mex_home/" + TODAY;
    
    private StringBuffer sHtml = new StringBuffer();

    private String strContentName = "";

    public makeHomeForMex(){
        Logger.getLogger("home.mex_home").info("( * ) Start.");
        init();
        Logger.getLogger("home.mex_home").info("( * ) End.");
    }

    public void init(){
        BufferedReader bfrString = null;
        StringBuffer sbfXml = new StringBuffer();

        Document doc = null;
        NodeList nlTemp = null;
        ByteArrayInputStream is = null;
        try{
            //HTTP Connection
            URLConnection urlCon = new URL("http://premium.mx.samsungmobile.com/xml/GetAllContentList.jsp?NUM=" + ILENG).openConnection();

            bfrString = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "UTF-8"));

            String line = "";
            Vector vtElem        = new Vector();
            Vector vRingtone     = new Vector();
            Vector vWallpapers   = new Vector();
            Vector vScreensavers = new Vector();
            Vector vGames        = new Vector();

            while ((line = bfrString.readLine()) != null)
            {
                sbfXml.append(line);
            }

            byte bxml[] = sbfXml.toString().getBytes("UTF-8");
            is = new ByteArrayInputStream(bxml);

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            doc = docBuilder.parse(new InputSource(is));
            Element elem = doc.getDocumentElement();
            elem.normalize();

            nlTemp = elem.getChildNodes();

            for(int x=0;x<nlTemp.getLength();x++){
                if(nlTemp.item(x).getChildNodes().getLength() > 0){
                    vtElem.addElement(nlTemp.item(x).getChildNodes());
                }
            }

            for(int x=0;x<vtElem.size();x++){
                Element tmpElem = (Element)vtElem.elementAt(x);
                String strElem = tmpElem.getTagName();

                for (int i = 0; i < ILENG; i++) {
                    Properties pptNode = new Properties();

                    nlTemp = tmpElem.getElementsByTagName("CONTENT_ID");
                    pptNode.setProperty("CONTENT_ID", nlTemp.item(i).getFirstChild().getNodeValue());
                    nlTemp = tmpElem.getElementsByTagName("CONTENT_NAME");
                    pptNode.setProperty("CONTENT_NAME", nlTemp.item(i).getFirstChild().getNodeValue());
                    nlTemp = tmpElem.getElementsByTagName("PREVIEW_URL");
                    pptNode.setProperty("PREVIEW_URL", nlTemp.item(i).getFirstChild().getNodeValue());

                    if(strElem.equals("GAMES")){
                        vGames.addElement(pptNode);
                    }
                    else if(strElem.equals("RINGTONES")){
                        vRingtone.addElement(pptNode);
                    }
                    else if(strElem.equals("WALLPAPERS")){
                        vWallpapers.addElement(pptNode);
                    }
                    else if(strElem.equals("SCREENSAVERS")){
                        vScreensavers.addElement(pptNode);
                    }
              }
            }
                
            File fl = new File(FILE_PATH);

            if(!fl.isDirectory()){
                fl.mkdirs();
            }

            if(vRingtone.size() > 0){
                getRingtones(vRingtone);
            }

            if(vWallpapers.size() > 0){
                getWallpaper(vWallpapers);
            }

            if(vGames.size() > 0){
                getJavaGames(vGames);
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            Logger.getLogger("home.mex_home").info(ex.toString());
            ReportError.byEmail(MAILSUBJECT, ex.toString());
        }
        finally {
            if (bfrString != null) try { bfrString.close(); } catch (Exception e) {}
            if (is != null) try { is.close(); } catch (Exception e) {}
        }
    }

    //Make Ringtone File
    public void getRingtones(Vector pVector) throws Exception {
        OutputStreamWriter fw = null;

        try{
            sHtml.setLength(0);
            sHtml.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"199\">\n")
                 .append("  <tr>\n")
                 .append("    <td colspan=\"3\"><a href=\"javascript:goMenu('play','01','');\"><img src=\"/images/main_tt_01.gif\" border=\"0\"></a></td>\n")
                 .append("  </tr>\n")
                 .append("  <tr>\n")
                 .append("    <td colspan=\"3\" height=\"1\" bgcolor=\"#B2B2B2\"></td>\n")
                 .append("  </tr>\n");

            for(int i=0; i < 3; i++) {
                Properties pContents = (Properties) pVector.elementAt(i);

                strContentName = StringUtils.abbreviate(StringUtils.defaultString(pContents.getProperty("CONTENT_NAME")), 20);

                if ( i>0 ) {
                    sHtml.append("  <tr>\n")
                         .append("    <td colspan=\"3\" height=\"1\" bgcolor=\"#DBDADA\"></td>\n")
                         .append("  </tr>\n");
                }

                sHtml.append("  <tr bgcolor=\"#F5F5F5\">\n")
                     .append("   <td width=\"23\" height=\"23\" align=\"center\">"+(i+1)+"</td>\n")
                     .append("   <td width=\"144\"><a href=\"./play/goPremium.jsp?mcate_cd=A1&content_id=" + pContents.getProperty("CONTENT_ID") + "\" ><div style=\"padding-left:3px;\">"+strContentName+"</div></a></td>\n")
                     .append("    <td width=\"25\"><a href=\"#\" ONCLICK=\"javascript:window.open('play/popup_play.jsp?filepath="+pContents.getProperty("PREVIEW_URL")+"','new','location=no,directories=no,resizable=no,status=no,toolbar=no,menubar=no,width=144,height=106,scrollbars=auto');return false\">\n")
                     .append("        <img src=\"/images/butn_play_01.gif\" border=\"0\"></a></td>\n")
                     .append("  </tr>\n");
            }

            sHtml.append("  <tr>\n")
                 .append("    <td colspan=\"3\" height=\"1\" bgcolor=\"#B2B2B2\"></td>\n")
                 .append("  </tr>\n")
                 .append("  <tr>\n")
                 .append("  </tr>\n")
                 .append("  <tr>\n")
                 .append("    <td colspan=\"3\" align=\"right\" height=\"21\" valign=\"top\">\n")
                 .append("      <a href=\"javascript:goMenu('play','01','');\"><img src=\"/images/main_butn_more01.gif\" border=\"0\"></a>\n")
                 .append("    </td>\n")
                 .append("  </tr>\n")
                 .append("</table>\n");

            fw = new OutputStreamWriter(new FileOutputStream(FILE_PATH + "/ringtone_include.html", false), "UTF-8");
            fw.write(sHtml.toString());
            
            fw.close();
            fw = null;
        }
        finally{
            if (fw != null) try { fw.close(); } catch (Exception e) {}
        }
    }

    //Make Wallpaper File
    public void getWallpaper(Vector pVector) throws Exception {
        OutputStreamWriter fw = null;
        String strImageURL = "";
        
        try{
            sHtml.setLength(0);
            sHtml.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"199\">\n")
                 .append("    <tr>\n")
                 .append("        <td colspan=\"2\"><a href=\"javascript:goMenu('play','02','');\"><img src=\"/images/main_tt_04.gif\"></a></td>\n")
                 .append("    </tr>\n")
                 .append("    <tr>\n")
                 .append("        <td width=\"96\" valign=\"top\">\n")
                 .append("            <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n")
                 .append("            <tr>\n")
                 .append("                <td width=\"91\" height=\"98\" valign=\"top\" background=\"/images/colorimage_bg.gif\">\n")
                 .append("                    <table width=\"84\" height=\"91\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n")
                 .append("                        <tr>\n")
                 .append("                            <td width=\"7\" height=\"14\"></td>\n")
                 .append("                            <td width=\"74\">&nbsp;</td>\n")
                 .append("                        </tr>\n")
                 .append("                        <tr>\n")
                 .append("                            <td width=\"7\">&nbsp;</td>\n")
                 .append("                            <td>\n");

            if(pVector.size() > 0) {
                Properties pContents = (Properties) pVector.elementAt(0);
                strImageURL = pContents.getProperty("PREVIEW_URL");

                sHtml.append("          <a href=\"./play/goPremium.jsp?mcate_cd=08&content_id=" + pContents.getProperty("CONTENT_ID") + "\"><img src=\""+strImageURL+"\" width=\"77\" height=\"77\"></a>\n");
            }

            sHtml.append("                        </td>\n")
                 .append("                        </tr>\n")
                 .append("                    </table>\n")
                 .append("                </td>\n")
                 .append("            </tr>\n")
                 .append("            </table>\n")
                 .append("        </td>\n")
                 .append("        <td width=\"103\" valign=\"top\">\n")
                 .append("            <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n")
                 .append("                <tr>\n")
                 .append("                    <td width=\"103\" height=\"1\" bgcolor=\"B4B4B4\"></td>\n")
                 .append("                </tr>\n")
                 .append("                <tr>\n")
                 .append("                    <td height=\"78\" valign=\"top\">\n")
                 .append("                        <table height=\"64\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");

            for(int i=1 ; i<pVector.size() ; i++) {
                Properties pContents = (Properties) pVector.elementAt(i);

                strContentName = StringUtils.abbreviate(StringUtils.defaultString(pContents.getProperty("CONTENT_NAME")), 15);
                
                sHtml.append("                            <tr>\n")
                     .append("                                <td width=\"10\" valign=\"top\"><img src=\"/images/icon/main_icon_03.gif\" width=\"3\" height=\"5\" alt=\"\" border=\"0\" align=\"absmiddle\" vspace=\"5\"></td>\n")
                     .append("                                <td width=\"102\"><a href=\"./play/goPremium.jsp?mcate_cd=08&content_id=" + pContents.getProperty("CONTENT_ID") + "\">"+strContentName+"</a></td>\n")
                     .append("                            </tr>\n");
            }

            sHtml.append("                        </table>\n")
                 .append("                    </td>\n")
                 .append("                </tr>\n")
                 .append("                <tr>\n")
                 .append("                    <td height=\"16\" align=\"right\"><a href=\"javascript:goMenu('play','02','');\"><img src=\"/images/main_butn_more01.gif\" border=\"0\"></a></td>\n")
                 .append("                </tr>\n")
                 .append("                <tr>\n")
                 .append("                    <td height=\"1\" bgcolor=\"B4B4B4\"></td>\n")
                 .append("                </tr>\n")
                 .append("            </table>\n")
                 .append("        </td>\n")
                 .append("    </tr>\n")
                 .append("</table>\n");
            
            fw = new OutputStreamWriter(new FileOutputStream(FILE_PATH + "/wallpaper_include.html", false), "UTF-8");
            fw.write(sHtml.toString());
            
            fw.close();
            fw = null;
        }
        finally{
            if (fw != null) try { fw.close(); } catch (Exception e) {}
        }
    }


    //Make JavaGames File
    public void getJavaGames(Vector pVector) throws Exception {
        OutputStreamWriter fw = null;

        try{
            sHtml.setLength(0);
            sHtml.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"199\">\n")
                 .append("    <tr>\n")
                 .append("        <td colspan=\"2\"><a href=\"javascript:goMenu('play','03','');\"><img src=\"/images/main_tt_03.gif\"></a></td>\n")
                 .append("    </tr>\n")
                 .append("    <tr>\n")
                 .append("       <td width=\"96\" valign=\"top\">\n")
                 .append("           <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n")
                 .append("           <tr>\n")
                 .append("               <td width=\"91\" height=\"98\" valign=\"top\" background=\"/images/mobilegame_bg.gif\">\n")
                 .append("                    <table width=\"84\" height=\"91\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n")
                 .append("                        <tr>\n")
                 .append("                            <td width=\"7\" height=\"14\"></td>\n")
                 .append("                            <td width=\"74\">&nbsp;</td>\n")
                 .append("                        </tr>\n")
                 .append("                        <tr>\n")
                 .append("                            <td width=\"7\">&nbsp;</td>\n");

            String strImageURL = "";
            if(pVector.size() > 0) {
                Properties pContents = (Properties) pVector.elementAt(0);
                strImageURL = pContents.getProperty("PREVIEW_URL");

                sHtml.append("<td><a href=\"./play/goPremium.jsp?mcate_cd=04&content_id=" + pContents.getProperty("CONTENT_ID") + "\"><img src=\""+strImageURL+"\" width=\"77\" height=\"77\"></a></td>\n");
            }else {
                sHtml.append("  <td></td>\n");
            }

            sHtml.append("                        </tr>\n")
                 .append("                    </table>\n")
                 .append("                </td>\n")
                 .append("            </tr>\n")
                 .append("            </table>\n")
                 .append("        </td>\n")
                 .append("        <td width=\"103\" valign=\"top\">\n")
                 .append("            <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n")
                 .append("                <tr>\n")
                 .append("                    <td width=\"103\" height=\"1\" bgcolor=\"B4B4B4\"></td>\n")
                 .append("                </tr>\n")
                 .append("                <tr>\n")
                 .append("                    <td height=\"78\" valign=\"top\">\n")
                 .append("                        <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");

            for(int i=1 ; i<pVector.size() ; i++) {
                Properties pContents = (Properties) pVector.elementAt(i);

                strContentName = StringUtils.abbreviate(StringUtils.defaultString(pContents.getProperty("CONTENT_NAME")), 15);
                
                sHtml.append("                            <tr>\n")
                     .append("                                <td width=\"10\" height=\"18\" valign=\"top\"><img src=\"/images/icon/main_icon_03.gif\" width=\"3\" height=\"5\" alt=\"\" border=\"0\" align=\"absmiddle\" vspace=\"5\"></td>\n")
                     .append("                                <td width=\"102\"><a href=\"./play/goPremium.jsp?mcate_cd=04&content_id=" + pContents.getProperty("CONTENT_ID") + "\">"+strContentName+"</a></td>\n")
                     .append("                            </tr>\n");
            }

            sHtml.append("                        </table>\n")
                 .append("                    </td>\n")
                 .append("                </tr>\n")
                 .append("                <tr>\n")
                 .append("                    <td height=\"16\" align=\"right\"><a href=\"javascript:goMenu('play','03','');\"><img src=\"/images/main_butn_more01.gif\" border=\"0\"></a></td>\n")
                 .append("                </tr>\n")
                 .append("                <tr>\n")
                 .append("                    <td height=\"1\" bgcolor=\"B4B4B4\"></td>\n")
                 .append("                </tr>\n")
                 .append("            </table>\n")
                 .append("        </td>\n")
                 .append("    </tr>\n")
                 .append("</table>\n");
            
            fw = new OutputStreamWriter(new FileOutputStream(FILE_PATH + "/javagames_include.html", false), "UTF-8");
            fw.write(sHtml.toString());
            
            fw.close();
            fw = null;
        }
        finally{
            if (fw != null) try { fw.close(); } catch (Exception e) {}
        }
    }

    public static void main( String[] args ){

        makeHomeForMex mvm = new makeHomeForMex();

    }
}